package com.artezio.buyndrive.validator;

import com.artezio.buyndrive.domain.Carservice;
import com.artezio.buyndrive.service.CarserviceService;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * User: spolyakov
 * Date: 25.07.12
 * Time: 16:52
 */

@Component
public class CarServiceRegisterValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return Carservice.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {

        Carservice carservice = (Carservice) target;

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "string.notEmpty");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address", "string.notEmpty");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone", "string.notEmpty");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "string.notEmpty");
        
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "idCity.id", "string.notEmpty");
        
        if(carservice.getName().length() > 200){
            errors.rejectValue("name", "string.tooLong");
        }
        
        if(carservice.getAddress().length() > 500){
            errors.rejectValue("address", "string.tooLong");
        }
        
        if(carservice.getPhone().length() > 300){
            errors.rejectValue("phone", "string.tooLong");
        }
        
        if(carservice.getDescription().length() > 2000){
            errors.rejectValue("description", "string.tooLong");
        }


    }
}
